import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:hong_vpn_new_20240626/utils/configs/text_style_config.dart';

import '../../utils/configs/color_config.dart';

class ChatTextFieldWidget extends StatefulWidget {
  final Function()? beginEditTap;
  final bool Function(String text)? sendTap;
  final TextEditingController textEditingController;
  const ChatTextFieldWidget(
      {super.key,
      this.beginEditTap,
      this.sendTap,
      required this.textEditingController});

  @override
  State<ChatTextFieldWidget> createState() => _ChatTextFieldWidgetState();
}

class _ChatTextFieldWidgetState extends State<ChatTextFieldWidget> {
  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  _beginEditTap() {
    if (widget.beginEditTap != null) {
      widget.beginEditTap!();
    }
  }

  _sendTap() {
    if (widget.textEditingController.text.isNotEmpty &&
        widget.sendTap != null) {
      var isSend = widget.sendTap!(widget.textEditingController.text);
      if (isSend) {
        widget.textEditingController.clear();
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
        child: Container(
      margin: const EdgeInsets.all(15),
      padding: const EdgeInsets.only(left: 6, right: 6),
      decoration: BoxDecoration(
          color: ColorConfig.bg,
          border: Border.all(color: ColorConfig.orange),
          borderRadius: const BorderRadius.all(Radius.circular(15))),
      child: Row(
        children: [
          Expanded(
              child: CupertinoTextField.borderless(
            controller: widget.textEditingController,
            onTap: () => _beginEditTap(),
            onEditingComplete: () => _sendTap(),
            maxLines: 1,
            keyboardType: TextInputType.multiline,
            textInputAction: TextInputAction.send,
            style: TextStyleConfig.middle,
            placeholder: "Please enter you question",
          )),
          IconButton(
            onPressed: () => _sendTap(),
            icon: const Icon(Icons.send),
            style: const ButtonStyle(
              alignment: Alignment.center,
              backgroundColor: WidgetStatePropertyAll(ColorConfig.orange),
              fixedSize: WidgetStatePropertyAll(Size(60, 25)),
            ),
          )
        ],
      ),
    ));
  }
}
